/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Homework3;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BoxLayout;

/**
 *
 * @author Jakim
 */
public class EventsHomework extends Frame {

    public Button btnRed;
    public Button btnGreen;
    public Button btnBlue;
    public Button btnClear;
    public Button btnClose;

    public Panel panelControls;
    public Panel panelCanvas;

    public TextField txtXCoords;
    public TextField txtYCoords;

    public Label lblCoords;

    public EventsHomework(){

        this.panelCanvas = new Panel();
        this.panelCanvas.setPreferredSize(new Dimension(500, 400));
        this.panelCanvas.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                Panel canvas = ((Panel) e.getSource());
                EventsHomework frame = ((EventsHomework) canvas.getParent());
                Graphics g = canvas.getGraphics();
                g.setColor(Color.black);
                g.drawOval(e.getX() - 3, e.getY() - 3, 5, 5);
                g.fillOval(e.getX() - 3, e.getY() - 3, 5, 5);
                frame.txtXCoords.setText(Integer.toString(e.getX()));
                frame.txtYCoords.setText(Integer.toString(e.getX()));
            }

        });

        this.panelControls = new Panel();
        this.panelControls.setLayout(new BoxLayout(this.panelControls, BoxLayout.PAGE_AXIS));
        this.panelControls.setPreferredSize(new Dimension(120, 400));

        FlowLayout layout = new FlowLayout(FlowLayout.LEFT);
        this.setLayout(layout);

        this.btnRed = new Button("Red");
        this.btnRed.setSize(120, 50);
        this.btnRed.addActionListener(new ChangeColorAction(this));

        this.btnGreen = new Button("Green");
        this.btnGreen.setSize(120, 50);
        this.btnGreen.addActionListener(new ChangeColorAction(this));

        this.btnBlue = new Button("Blue");
        this.btnBlue.setSize(120, 50);
        this.btnBlue.addActionListener(new ChangeColorAction(this));

        this.btnClear = new Button("Clear");
        this.btnClear.setSize(120, 50);
        this.btnClear.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                Panel panelContrls =
                        (Panel) ((Button) e.getSource()).getParent();

                EventsHomework frame =
                        (EventsHomework) panelContrls.getParent();

                frame.panelCanvas.repaint();
                frame.txtXCoords.setText("");
                frame.txtYCoords.setText("");

            }
        });

        this.btnClose = new Button("Close");
        this.btnClose.setSize(120, 50);
        this.btnClose.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        this.txtXCoords = new TextField();
        this.txtXCoords.setSize(120, 50);

        this.txtYCoords = new TextField();
        this.txtYCoords.setSize(120, 50);

        this.lblCoords = new Label("Coordinates");

        this.panelControls.add(this.btnRed);
        this.panelControls.add(this.btnGreen);
        this.panelControls.add(this.btnBlue);
        this.panelControls.add(this.btnClear);
        this.panelControls.add(this.btnClose);
        this.panelControls.add(this.lblCoords);
        this.panelControls.add(this.txtXCoords);
        this.panelControls.add(this.txtYCoords);
    
        this.add(this.panelControls);
        this.add(this.panelCanvas);
        this.pack();
        this.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        this.setVisible(true);
        this.setMinimumSize(layout.preferredLayoutSize(this));
    }

     private class ChangeColorAction implements ActionListener{

         private Panel _panelToChange;
         private Color _color;

            public ChangeColorAction(EventsHomework frame){
                super();
                this._panelToChange = frame.panelCanvas;
            }

            public void actionPerformed(ActionEvent e) {
                String buttonPressed = ((Button) e.getSource()).getLabel();
                if (buttonPressed.equals("Red")){
                    this._color = Color.red;
                    this._panelToChange.setBackground(this._color);
                }

                if (buttonPressed.equals("Green")){
                    this._color = Color.green;
                    this._panelToChange.setBackground(this._color);
                }

                if (buttonPressed.equals("Blue")){
                    this._color = Color.blue;
                    this._panelToChange.setBackground(this._color);
                }
            }

        }

     public static void main(String args[]){
        new  EventsHomework();
     }

}
